//
//  OffDetailViewController.m
//  MobileOffice
//
//  Created by jjkj on 16/11/1.
//  Copyright © 2016年 liaoningjujin. All rights reserved.
//

#import "OffDetailViewController.h"

@interface OffDetailViewController ()

@property (nonatomic,strong) UILabel *personYearLabel;
@end

@implementation OffDetailViewController

#pragma mark -调休申请
- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Do any additional setup after loading the view.
}

#pragma mark -初始化数据
- (void)dataInit{
    self.mustSelectArr=[NSMutableArray new];
    self.addWorkPersonArr=[NSMutableArray new];
    _titleArr=@[@"申  请  人 :",@"开始时间 :",@"结束时间 :",@"调休天数 :"];
    _messageArr=[NSMutableArray arrayWithArray:@[@"",self.beginStr?self.beginStr:@"点击选择",self.endStr?self.endStr:@"点击选择",@"0  天"]];
    if (self.beginStr&&self.endStr) {
        [self getVacateDaysOnlyRequest];
    }
}


//#pragma mark -初始化数据
//- (void)dataInit{
//    NSArray *array = [NSArray arrayWithObjects:@"a art.一(个)；每一(个)",
//                      @"abandon vt.丢弃；放弃，抛弃",
//                      @"ability n.能力；能耐，本领",
//                      @"able a.有能力的；出色的",
//                      @"abnormal a.不正常的；变态的",
//                      @"aboard ad.在船(车)上；上船",
//                      @"about prep.关于；在…周围",
//                      @"above prep.在…上面；高于",
//                      @"abroad ad.(在)国外；到处",
//                      @"absence n.缺席，不在场；缺乏",
//                      @"absent a.不在场的；缺乏的",
//                      @"absolute a.绝对的；纯粹的",
//                      @"absolutely ad.完全地；绝对地",
//                      @"absorb vt.吸收；使专心",
//                      @"abstract a.抽象的 n.摘要",
//                      @"abundant a.丰富的；大量的",
//                      @"abuse vt.滥用；虐待 n.滥用",
//                      @"academic a.学院的；学术的",
//                      @"academy n.私立中学；专科院校",
//                      @"accelerate vt.(使)加快；促进",
//                      @"acceleration n.加速；加速度",
//                      @"accent n.口音，腔调；重音",
//                      @"accept vt.vi.接受；同意",
//                      @"acceptable a.可接受的，合意的",
//                      @"acceptance n.接受，验收；承认",
//                      @"access n.接近；通道，入口",
//                      @"accessory n.同谋，从犯；附件",
//                      @"accident n.意外的；事故",
//                      @"accidental a.偶然的；非本质的",
//                      @"accommodate vt.容纳；供应，供给",
//                      @"accommodation n.招待设备；预定铺位",
//                      @"accompany vt.陪伴，陪同；伴随",
//                      @"accomplish vt.达到(目的)；完成",
//                      @"accord vt.使一致；给予",
//                      @"accordance n.一致；和谐；授予",
//                      @"accordingly ad.因此，所以；照着",
//                      @"account n.记述；解释；帐目",
//                      @"accumulate vt.积累 vi.堆积",
//                      @"accuracy n.准确(性)；准确度",
//                      @"accurate a.准确的，正确无误的",
//                      @"accuse vt.指责；归咎于",
//                      @"accustom vt.使习惯",
//                      @"accustomed a.惯常的；习惯的",
//                      @"ache vi.痛；想念 n.疼痛",
//                      @"achieve vt.完成，实现；达到",
//                      @"achievement n.完成；成就，成绩",
//                      @"acid n.酸；酸的，酸性的",
//                      @"acquaintance n.认识；了解；熟人",
//                      @"acquire vt.取得；获得；学到",
//                      @"acre n.英亩(=6.07亩)",
//                      @"across prep.横过；在…对面",
//                      @"act vi.行动；见效 n.行为",
//                      @"action n.行动；作用；功能",
//                      @"active a.活跃的；积极的",
//                      @"activity n.活动；活力；行动",
//                      @"actor n.男演员；演剧的人",
//                      @"actress n.女演员",
//                      @"actual a.实际的；现行的",
//                      @"actually ad.实际上；竟然",
//                      @"acute a.尖的，锐的；敏锐的",
//                      @"ad n.广告",
//                      @"adapt vt.使适应；改编",
//                      @"add vt.添加，附加，掺加",
//                      @"addition n.加，加法；附加物",
//                      @"additional a.附加的，追加的",
//                      @"address n.地址；演说；谈吐",
//                      @"adequate a.足够的；可以胜任的",
//                      @"adjective n.形容词 a.形容词的",
//                      @"adjust vt.调整，调节；校正",
//                      @"administration n.管理；管理部门",
//                      @"admire vt.钦佩，羡慕，赞赏",
//                      @"admission n.允许进入；承认",
//                      nil];
//        self.mustSelectArr=[NSMutableArray new];
//        self.addWorkPersonArr=[NSMutableArray new];
//        _titleArr=@[@"申  请  人 :",@"开始时间 :",@"结束时间 :",@"调休天数 :"];
//        _messageArr=[NSMutableArray arrayWithArray:@[@"",@"点击选择",@"点击选择",@"0  天"]];
//    self.reasonList = [[ReasonListTableViewController alloc] initWithArray:array];
//}

- (void)addTableViewFootView{
    //添加提交和存草稿button
    UIView *footView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, IPHONE_WIDTH, 80)];
    footView.backgroundColor=[UIColor clearColor];
    
    NSArray *title=@[@"确定"];
    CGFloat buttonWidth=(IPHONE_WIDTH-(title.count+1)*10)/title.count;
    for (int i=0; i<title.count; i++) {
        UIButton *submitButton=[MyControl creatButtonWithFrame:CGRectMake(10*(i+1)+buttonWidth*i,20, buttonWidth, 40) target:self sel:@selector(submitClick:) tag:120+i image:nil title:title[i]];
        submitButton.layer.masksToBounds=YES;
        submitButton.layer.cornerRadius=5;
        [submitButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        if (i==0) {
            submitButton.backgroundColor=COLOR_MIDBLUE;
        }else{
            submitButton.backgroundColor=[UIColor orangeColor];
        }
        [footView addSubview:submitButton];
    }
    _tableView.tableFooterView=footView;
}

#pragma mark -textViewDelegate

- (void)textViewDidBeginEditing:(UITextView *)textView{
    [self setLabelBorderColorWithLabel:self.reasonTitleLabel color:[UIColor clearColor]];
    NSIndexPath* path = [NSIndexPath indexPathForRow:0 inSection:1];
    [self performSelector:@selector(scrollToCell:) withObject:path afterDelay:0];
}

-(void) scrollToCell:(NSIndexPath*) path {
    [_tableView scrollToRowAtIndexPath:path atScrollPosition:UITableViewScrollPositionTop animated:YES];
}

#pragma mark - TableViewDelagate

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section==0) {
        return 4;
    }else if (section==2){
        return self.addWorkPersonArr.count+1;
    }
    return 1;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section==0) {
//        if (indexPath.row==0) {
//            NSString *cellID=@"DutyBasicTableViewCell";
//            DutyBasicTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellID];
//            if (!cell) {
//                cell=[[DutyBasicTableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellID];
//                cell.selectionStyle=UITableViewCellSelectionStyleNone;
//            }
//            cell.myTitleLabel.text=@"申  请  人 :";
//            self.userTitleLabel=cell.myTitleLabel;
//            cell.messageLabel.text=[NSString stringWithFormat:@"%@ ( %@ )",[LZXHelper getUserName],[LZXHelper getUserDepartName]];
//            return cell;
//
//        }else{
        //
        NSString *cellID=@"DutyBasicTableViewCell";
        DutyBasicTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellID];
        if (!cell) {
            cell=[[DutyBasicTableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellID];
            cell.selectionStyle=UITableViewCellSelectionStyleNone;
            if (indexPath.row==1) {
                [cell.signButton addTarget:self action:@selector(signButtonClick:) forControlEvents:UIControlEventTouchUpInside];
            }
        }
        cell.myTitleLabel.text=_titleArr[indexPath.row];
        //保存疑问按钮
        if (indexPath.row==1) {
            self.beginSignButton=cell.signButton;
            self.beginTitleLabel=cell.myTitleLabel;
        }else if (indexPath.row==2){
            self.endSignButton=cell.signButton;
            self.endTitleLabel=cell.myTitleLabel;
        }
        if (indexPath.row==0) {
            cell.messageLabel.text=[NSString stringWithFormat:@"%@ ( %@ )",[LZXHelper getUserName],[LZXHelper getUserDepartName]];
        }else{
            cell.messageLabel.text=_messageArr[indexPath.row];
        }

        if (indexPath.row==1||indexPath.row==2) {
            cell.arrowImageView.hidden=NO;
            if ([cell.messageLabel.text isEqualToString:@"点击选择"]) {
                cell.messageLabel.textColor=[UIColor lightGrayColor];
            }else{
                cell.messageLabel.textColor=[UIColor blackColor];
            }
            
        }
        if (indexPath.row==3) {
            self.holidayCountLabel=cell.messageLabel;
            self.holidaysTitleLabel=cell.myTitleLabel;
        }
        return cell;
 //       }
    }else if(indexPath.section==1){
            NSString *cellID=@"ReasonTextTableViewCell";
            ReasonTextTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellID];
            if (!cell) {
                cell=[[ReasonTextTableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellID title:@" 请填/选请假事由 : " noticeStr:nil];
                cell.selectionStyle=UITableViewCellSelectionStyleNone;
            }
            self.reasonTextView=cell.reasonTextView;
            self.reasonTextView.delegate=self;
            self.reasonTitleLabel=cell.myTitleLabel;
            return cell;
    }else{
        if (indexPath.row==0) {
            NSString *cellID=@"CenterLabelTableViewCell";
            CenterLabelTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellID];
            if (!cell) {
                cell=[[CenterLabelTableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellID];
                cell.selectionStyle=UITableViewCellSelectionStyleNone;
            }
            cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
            cell.myTitle.text=@"点击此处添加工作委托";
            return cell;
        }else{
            NSString *cellID=@"AddPersonListTableViewCell";
            AddPersonListTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellID];
            if (!cell) {
                cell=[[AddPersonListTableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellID];
                cell.selectionStyle=UITableViewCellSelectionStyleNone;
            }
            DepartPersonListModel *model=self.addWorkPersonArr[indexPath.row-1];
            cell.myTitleLabel.text=model.personName;
            cell.messageLabel.text=[NSString stringWithFormat:@"%@ 至 %@",model.beginStr,model.endStr];
            return cell;
        }
    }
    //    [cell.headImageView setImage:[UIImage imageNamed:_imageArr[indexPath.section][indexPath.row]]];
    //    cell.myTitle.text=_titleArr[indexPath.section][indexPath.row];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [self.reasonTextView resignFirstResponder];
    if (indexPath.section==0) {
        if (indexPath.row==1||indexPath.row==2){
            NSString *dateStr=nil;
            if (indexPath.row==1) {
                if (self.beginStr) {
                    dateStr=self.beginStr;
                }else if (self.endStr){
                    dateStr=self.endStr;
                }
            }
            if (indexPath.row==2) {
                if (self.endStr) {
                    dateStr=self.endStr;
                }else if (self.beginStr){
                    dateStr=self.beginStr;
                }
            }
            DatePickerView *datePickerView=[[DatePickerView alloc]initWithFrame:CGRectMake(0, 0, IPHONE_WIDTH, IPHONE_HEIGHT)nowDateStr:dateStr];
            __weak NSMutableArray *myMessageArr=_messageArr;
            __weak UITableView *myTableView=_tableView;
            __weak DutyParentsViewController *mySelf=self;
            datePickerView.sureBlock=^(NSString *timeStr){
                if (indexPath.row==1) {
                    //如果是选择开始时间
                    //先判断有没有结束时间,如果有要进行比对
                    if (mySelf.endStr) {
                        if (![mySelf compareOneTime:timeStr withAnotherTime:mySelf.endStr]) {
                            [LZXHelper postMessageWithText:@"请假结束时间应该在请假开始时间之后!" View:mySelf.view];
                        }
                        [mySelf getVacateDaysOnlyRequest];
                    }
                    if (mySelf.dutyType==ApplyHoliday) {
                        //如果是请假
                        if ([mySelf compareTime:timeStr]) {
                            //比现在的时间早
                            [LZXHelper postAlertViewControllerWithTitle:@"您选择的请假开始时间早于今天,要补假吗?" message:nil cancelButtonTitle:@"选错时间了" otherButtonTitle:@"去补假" cancelEvent:^{
                                
                            } otherEvent:^{
                                mySelf.dutyType=1;
                                
                                [mySelf setNavigationTitle:[mySelf.navaTitle stringByAppendingString:@"补假申请"]];
                                
                            } viewController:mySelf];
                        }else{
                            //比现在的时间晚
                            mySelf.beginStr=[timeStr copy];
                            
                            [mySelf setLabelBorderColorWithLabel:mySelf.beginTitleLabel color:[UIColor clearColor]];
                            [myMessageArr replaceObjectAtIndex:indexPath.row withObject:timeStr];
                            [myTableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
                            [mySelf getTimeControlRequestWithBeginTime:[timeStr copy]endTime:nil type:@"Begin"];
                        }
                        
                    }else{
                        //如果是补假
                        if ([mySelf compareTime:timeStr]){
                            //比现在的时间早
                            mySelf.beginStr=[timeStr copy];
                            [mySelf setLabelBorderColorWithLabel:mySelf.beginTitleLabel color:[UIColor clearColor]];
                            [myMessageArr replaceObjectAtIndex:indexPath.row withObject:timeStr];
                            [myTableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
                            [mySelf getTimeControlRequestWithBeginTime:[timeStr copy]endTime:nil type:@"Begin"];
                        }else{
                            //比现在的时间晚
                            [LZXHelper postAlertViewControllerWithTitle:@"您选择的请假开始时间晚于今天,要请假吗?" message:nil cancelButtonTitle:@"选错时间了" otherButtonTitle:@"去请假" cancelEvent:^{
                                
                            } otherEvent:^{
                                mySelf.dutyType=0;
                                [mySelf setNavigationTitle:[mySelf.navaTitle stringByAppendingString:@"申请"]];
                            } viewController:mySelf];
                        }
                    }
                    
                }else{
                    //点击结束时间
                    mySelf.endStr=[timeStr copy];
                    [mySelf setLabelBorderColorWithLabel:mySelf.endTitleLabel color:[UIColor clearColor]];
                    [myMessageArr replaceObjectAtIndex:indexPath.row withObject:timeStr];
                    [myTableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
                    [mySelf getVacateDaysOnlyRequest];
                    [mySelf getTimeControlRequestWithBeginTime:mySelf.beginStr endTime:[timeStr copy] type:@"End"];
                }
            };
            [[[UIApplication sharedApplication].delegate window] addSubview:datePickerView];
        }
    }else if (indexPath.section==2&&indexPath.row==0){
        if (!self.beginStr) {
            [LZXHelper postMessageWithText:@"请假开始时间不能为空!" View:self.view];
            return;
        }
        if (!self.endStr) {
            [LZXHelper postMessageWithText:@"请假结束时间不能为空!" View:self.view];
            return;
        }
        AddWorkPersonViewController *person=[AddWorkPersonViewController new];
        person.dutyType=self.dutyType;
        person.beginStr=self.beginStr;
        person.endStr=self.endStr;
        __weak UITableView *myTableView=_tableView;
        person.myBlock=^(DepartPersonListModel *model){
            [self.addWorkPersonArr addObject:model];
            [myTableView reloadSections:[NSIndexSet indexSetWithIndex:indexPath.section]  withRowAnimation:UITableViewRowAnimationFade];
        };
        [self.navigationController pushViewController:person animated:YES];
    }else{
        
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (section==1) {
        UIView *backView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, IPHONE_WIDTH, 50)];
        backView.backgroundColor=[UIColor whiteColor];
        
        self.personYearLabel=[MyControl creatLabelWithFrame:CGRectMake(10, 5, IPHONE_WIDTH-20, 30) text:[NSString stringWithFormat:@"可用调休天数%.1f天",[self.personYearDays floatValue]]];
        self.personYearLabel.textAlignment=NSTextAlignmentRight;
        //self.personYearLabel.font=[UIFont boldSystemFontOfSize:17];
        [backView addSubview:self.personYearLabel];
        
        //添加分割线
        UILabel *line=[[UILabel alloc]initWithFrame:CGRectMake(0, 49, IPHONE_WIDTH, 1)];
        line.backgroundColor=BACKCOLOR_GRAY;
        [backView addSubview:line];
        return backView;
    }
    
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section==1) {
        return 50;
    }
    return 10;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    //如果是添加工作委托
    if (indexPath.section==2) {
        if (indexPath.row==0) {
            return 50;
        }else{
            return 64;
        }
    }else if (indexPath.section==1){
        return 100;
    }
    return 50;
}

- (void)getPersonalDaysInfoSuccess{
    self.personYearLabel.text=[NSString stringWithFormat:@"可用调休天数%.1f天",[self.personYearDays floatValue]];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
